package com.wondertek.mam.tag.aop.logging;

import com.google.common.base.Charsets;
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import com.wondertek.mam.tag.config.Constant;
import com.wondertek.mam.tag.wordSource.bean.NetHotWord;
import com.wondertek.mam.tag.wordSource.impl.BaiduHotTodayWordSource;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wangdongxu on 2017-09-13 上午 9:44:36.
 */
public class T {
    private static final Logger LOGGER = LoggerFactory.getLogger(T.class);

    public static void main(String[] args) throws IOException {
        try {

        File thesaurusFile = new File("D:\\a\\b\\a.txt");
        if (!thesaurusFile.exists()){
            mkDir(thesaurusFile.getParentFile());
        }
                FileOutputStream fos = new FileOutputStream(thesaurusFile);
                    try {
                        IOUtils.write("abc", fos, Charsets.UTF_8);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

        } catch(FileNotFoundException e){
            LOGGER.error("创建词库文件时出错");
            e.printStackTrace();
        }
        //test2();
        //test1();
        //NetHotWord hotWordsByUrl = getHotWordsByUrl("http://top.baidu.com/buzz?b=499", 10000);
        //System.out.println(hotWordsByUrl);
    }

    public static void mkDir(File file) {
        if (file.getParentFile().exists()) {
            file.mkdir();
        } else {
            mkDir(file.getParentFile());
            file.mkdir();
        }
    }
    //Unicode转中文
    public static String decodeUnicode(String dataStr) {
        int start = dataStr.indexOf("\\u");
        while (start != -1) {
            String subStr = dataStr.substring(start + 2, start + 6);
            char letter = (char) Integer.parseInt(subStr, 16);
            dataStr = dataStr.replaceAll("\\\\u" + subStr, String.valueOf(letter));
            start = dataStr.indexOf("\\u");
        }
        return dataStr;

    }

    public static void test() {
        //String str ="\\u56fd东\\u5e86\\u516b\\u5929王\\u53c2a\\u52a0\\u573a\\u5a5a\\u793c";
        //System.out.println("\u56fd\u5e86\u516b\u5929\u53c2\u52a06\u573a\u5a5a\u793c");
        try {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.39", 3128));
            Document doc = Jsoup.connect("http://s.weibo.com/top/summary?cate=total&key=all").proxy(proxy).get();
            String script = doc.getElementsByTag("script").get(14).html();
            String substring = script.substring(script.indexOf("\"html\":\"") + 8, script.indexOf("\"}"));
            String[] split = substring.split("(list_person\\\\\">)|(list_films\\\\\">)|(list_all\\\\\">)|(list_realtimehot\\\\\">)|(ad_hotword_realtime_[0-9]*\\\\\"\\\\n>)");
            List<String> splitList = new ArrayList();
            for (int i = 0; i < split.length; i++) {
                if (i % 2 == 1) splitList.add(split[i]);
            }
            splitList.stream().forEach(s -> System.out.println(s));
            splitList.parallelStream().map(s -> s.split("<\\\\/a>")[0]).forEach(s -> {
                while (true) {
                }
            });
            System.out.println("-----------");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test1() {
        String url = "http://top.baidu.com/buzz?b=2";
        try {
            Document doc = Jsoup.connect(url).get();
            //Elements elementsByClass =doc.getElementsByClass("list-title");
            Elements topTitleEle = doc.getElementsByClass("top-title");
            System.out.println(doc.getElementsByTag("tbody").get(0).getElementsByTag("tr").get(0).getElementsByClass("list-title").size() == 0);
            doc.getElementsByTag("tbody").get(0).getElementsByTag("tr").stream().filter(element -> element.getElementsByClass("list-title").size() != 0)
                .map(ele -> {
                    String word = ele.getElementsByClass("list-title").html();
                    Integer score = Integer.parseInt(ele.getElementsByClass("last").get(0).getElementsByTag("span").html());
                    return word + score;
                }).forEach(System.out::println);
            //List<String> collect = elementsByClass.stream().map(element -> element.html()).collect(Collectors.toList());
            //String keyWord = topTitleEle.get(0).getElementsByTag("h2").html().replaceAll("排行榜", "").replaceAll("今日", "");
            //return new NetHotWord(keyWord,collect);
        } catch (IOException e) {
            //LOGGER.error("获取百度热点出现错误:"+url);
        } catch (Exception e) {
            System.out.println("err----" + url);
        }
    }

    public static NetHotWord getHotWordsByUrl(String url, Integer hotThreshold) {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements topTitleEle = doc.getElementsByClass("top-title");
            String keyWord = topTitleEle.get(0)
                .getElementsByTag("h2")
                .html()
                .replaceAll("排行榜", "")
                .replaceAll("今日", "");
            //System.out.println(doc.getElementsByTag("tbody").get(0).getElementsByTag("tr"));
            if (doc.getElementsByTag("tbody").size()==0) return null;
            Map<String, Boolean> collect = doc.getElementsByTag("tbody").get(0).getElementsByTag("tr").stream()
                .filter(element -> element.getElementsByClass("list-title").size() != 0)
                .map(ele -> {
                    String word = ele.getElementsByClass("list-title").attr("href").split("(&w=)|(&wd=)")[1];
                    word = URLDeCodeGBK(word);
                    System.out.println(word);
                    Integer score;
                    try {
                        score = Integer.parseInt(ele.getElementsByClass("last").get(0).getElementsByTag("span").html());
                    } catch (NumberFormatException e) {
                        score = 0;
                    }
                    if (score >= hotThreshold) {
                        return "1" + word;
                    }
                    return "0" + word;
                })
                .collect(Collectors.toMap(key -> key.substring(1), value -> value.substring(0, 1).equals("1") ? true : false));
            return new NetHotWord(keyWord, collect);
        } catch (IOException e) {
            LOGGER.error("获取百度热点出现错误:" + url);
        } catch (Exception e) {
            System.out.println("err----" + url);
        }
        return null;
    }

    public static String URLDeCodeGBK(String url) {
        try {
            List<String> hexStrs = Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e" ,"f", "A", "B", "C", "D", "E","F");
            //String url = "%c0coc%% %av%abfghjf8%ab";
            String newStr = "";
            String[] split = url.split("%");
            int sum = Arrays.asList(split).parallelStream().mapToInt(s -> s.length()).sum();
            int endB = url.length() - sum - split.length+1;

            for (int i = 0; i < split.length; i++) {
                String str = split[i];
                if (i == 0) {
                    //if (!newStr.equals("")) {
                        newStr += str;
                    //}
                    continue;
                }
                if (str.isEmpty()) {
                    newStr += URLEncoder.encode("%", "GBK");
                } else if (str.length() == 1) {
                    newStr += URLEncoder.encode("%" + str, "GBK");
                } else {
                    if (hexStrs.contains(str.substring(0, 1))&&hexStrs.contains(str.substring(1, 2))){
                        newStr = newStr + "%" + str.substring(0,2);
                    }else {
                        newStr = newStr + URLEncoder.encode("%" + str.substring(0,2),"GBK");
                    }
                    if (str.length()>2)
                        if (URLEncoder.encode(str.substring(2,3),"GBK").equals(str.substring(2,3))){
                            newStr = newStr + "%" +Integer.toHexString((int)str.charAt(2)) + str.substring(3);
                        }else {
                            newStr = newStr + str.substring(2);
                        }
                }

            }
            //System.out.println(newStr);
            String result = URLDecoder.decode(newStr, "GBK");
            for (int i = 0; i < endB; i++) {
                result+="%";
            }
            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}

